package com.sskjdata.cg.service.impl.auth;

import com.sskjdata.cg.jwt.JwtUserFactory;
import com.sskjdata.cg.po.sys.SysUser;
import com.sskjdata.cg.service.auth.JwtUserDetailsService;
import com.sskjdata.cg.service.impl.BaseServiceImpl;
import com.sskjdata.cg.service.sys.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Descirption jwt相关service用于验证用户
 * @Author Jason
 * @Date 2018/6/19/
 */
@Service
@Slf4j
public class JwtUserDetailsServiceImpl extends BaseServiceImpl implements UserDetailsService ,JwtUserDetailsService {

    @Autowired
    private SysUserService sysUserService;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        try {
            SysUser sysUser = sysUserService.findByUsername(username);
            return JwtUserFactory.create(String.valueOf(sysUser.getId()),username,sysUser.getPassword(),new Date());
        } catch (Exception e) {
            log.error("jwt验证过程根据用户名获取用户出错",e);
        }
        throw new UsernameNotFoundException("jwt auth something unexpected happened");
    }
}

